﻿using System;
using System.Windows;
using System.Windows.Controls;

using Game.Command;
using Game.Common;
using Game.Common.Resources;
using Game.Controller;
using Game.Level;
using Game.Map;
using Game.Map.Extensions;
using Game.Objects;
using Game.Toolkit;

namespace SL
{
    public partial class RightPanelControl 
    {
        IGameMapView _GameMap;
        CommandMenu _Menu;
        GameController _GameController;
        GameLevels _GameLevels;
        GameResources _GameResources;

        public RightPanelControl()
        {
            InitializeComponent();
        }

        public GameController GameController
        {
            get { return _GameController; }
            set
            {
                _GameController = value;
                _GameController.CommandExecutor.IsRunningChanged += OnIsRunningChanged;
            }
        }

        public GameLevels GameLevels
        {
            get { return _GameLevels; }
            set 
            { 
                _GameLevels = value; 
                _GameLevels.Events.LevelActive += delegate
                {
                    textBlock1.Text = "";
                    HidePopupMenu();
                };
            }
        }

        public IGameMapView GameMap
        {
            get { return _GameMap; }
            set 
            { 
                _GameMap=value;

                _GameMap.SelectedX().SelectedChanged += delegate
                {
                    textBlock1.Text = GetInfo();
                };

                _GameMap.SelectedX().SelectedChanged += delegate
                {
                    if (_GameMap.SelectedX().Selected == null)
                    {
                        HidePopupMenu();
                    }
                    else if (
                        _GameMap.SelectedX().Selected.VehicleX().Vehicle == null && 
                        _GameMap.SelectedX().Selected.ObjectX().GameObject == null)
                    {
                        HidePopupMenu();
                    }
                    else
                    {
                        DisplayPopupMenu();
                    }
                };
            }
        }

        public void GameEnded()
        {
            textBlock1.Text = "";
            HidePopupMenu();
            button1.Content = "Restart";
            _GameController = null;
        }

        string GetInfo()
        {
            if (_GameResources != null)
            {
                _GameResources.Changed -= OnChange;
            }

            if (_GameMap.SelectedX().Selected != null)
            {
                if (_GameMap.SelectedX().Selected.VehicleX().Vehicle != null)
                {
                    _GameResources = _GameMap.SelectedX().Selected.VehicleX().Vehicle.Tank;
                    _GameResources.Changed += OnChange;
                }
                else
                {
                    var obj = _GameMap.SelectedX().Selected.ObjectX().GameObject;
                    if (obj != null && obj.Building != null)
                    {
                        _GameResources = obj.Building.Tank;
                        _GameResources.Changed += OnChange;
                    }
                }
            }

            return BuildingPropertyPrinter.GetInfo(_GameMap);
        }

        void OnChange(object source, EventArgs args)
        {
            textBlock1.Text = GetInfo();
        }

        void HidePopupMenu()
        {
            if (_Menu != null)
            {
                grid3.Children.Remove(_Menu);
                _Menu = null;
            }
        }

        void DisplayPopupMenu()
        {
            var grid = grid3;

            if (_Menu != null)
            {
                grid.Children.Remove(_Menu);
            }

            var v = _GameMap.SelectedX().Selected.VehicleX().Vehicle;
            if (v != null && v.Owner == HumanPlayer.Player)
            {
                DisplayVehiclePopup(grid, v);
            }
            else
            {
                var obj = _GameMap.SelectedX().Selected.ObjectX().GameObject;
                if (obj != null)
                {
                    var building = obj.Building;
                    if (building != null && building.Owner == HumanPlayer.Player)
                    {
                        DisplayBuildingPopup(grid, building);
                    }
                }
            }
        }

        void DisplayBuildingPopup(Grid grid, GameBuilding building)
        {
            _Menu = new CommandMenu();
            grid.Children.Add(_Menu);
            Grid.SetColumn(_Menu, 0);
            Grid.SetRow(_Menu, 1);
            _Menu.VerticalAlignment = VerticalAlignment.Bottom;
            _Menu.HorizontalAlignment = HorizontalAlignment.Stretch;
            _Menu.Width = double.NaN;
            _Menu.Height = double.NaN;
            _Menu.Margin = new Thickness(0, 0, 0, 5);

            CommandQueueExtension ext = null;
            if (building.Extensions.Count > 0)
            {
                ext = building.Extensions.Find<CommandQueueExtension>();
                if (ext != null)
                {
                    var queueControl = new CommandQueueControl
                                           {
                                               VerticalAlignment = VerticalAlignment.Top
                                           };

                    queueControl.Attach(_GameController.CommandExecutor, ext.ScheduledCommandQueue);
                    _Menu.StackPanel.Children.Add(queueControl);

                    ext.ScheduledCommandQueue.CollectionChanged += (sender, e) => queueControl.Refresh(_GameController.CommandExecutor);
                }
            }

            var commands = new BuildingCommands(
                building, _GameMap, _GameMap.SelectedX().Selected);
            foreach (var command in commands.Commands)
            {
                var button = new ScheduleCommandButton(command) {Content = "Build"};
                SchedulerCommand command1 = command;
                button.Click += delegate
                {
                    if (ext != null)
                    {
                        var window = new BuildUnitDlg(
                            building, command1, ext, _GameController);
                        window.Show();
                    }

                    //if (ext != null)
                    //{
                    //    var turn = GameTurn.Create();
                    //    var cmd = command1.CreateScheduledCommand();
                    //    ext.ScheduledCommandQueue.Add(_GameController.CommandExecutor, cmd, turn);
                    //}
                };

                button.Padding = new Thickness(2);
                _Menu.StackPanel.Children.Add(button);
            }
        }

        void DisplayVehiclePopup(Grid grid, Vehicle v)
        {
            _Menu = new CommandMenu();
            grid.Children.Add(_Menu);
            Grid.SetColumn(_Menu, 0);
            Grid.SetRow(_Menu, 1);
            _Menu.VerticalAlignment = VerticalAlignment.Bottom;
            _Menu.HorizontalAlignment = HorizontalAlignment.Left;
            _Menu.Width = double.NaN;
            _Menu.Height = double.NaN;
            _Menu.Margin = new Thickness(0, 0, 0, 5);

            var commands = new VehicleCommands(v);
            foreach (var command in commands.Commands)
            {
                var button = new Button();
                new ButtonEnlarger(button, command, _GameController);
                button.Content = command.Name;
                var command1 = command;
                button.Click += delegate
                {
                    var turn = GameTurn.Create();
                    _GameController.CommandExecutor.Execute(
                        _GameMap, _GameController.GameLevels, turn, command1);

                    _GameController.ClearCommands();
                };

                button.Padding = new Thickness(10);
                _Menu.StackPanel.Children.Add(button);
            }
        }

        void OnIsRunningChanged(object source, EventArgs args)
        {
            if (_GameController.CommandExecutor.IsRunning)
            {
                button1.IsEnabled = false;
            }
            else
            {
                button1.IsEnabled = true;
            }
        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            if (_GameController != null)
            {
                GameTurn turn = GameTurn.Create();
                _GameController.ClearCommands();
                _GameController.DoTurn(turn);
            }
            else
            {
                _GameLevels.Events.FireGameRestart();
                button1.Content = "Next Turn";
            }
        }

    }
}
